<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getEmployee">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="username" label="用户名" min-width="80" sortable>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" min-width="110" sortable>
            </el-table-column>
            <el-table-column prop="phone" label="电话" min-width="110" sortable>
            </el-table-column>
            <el-table-column prop="age" label="年龄" min-width="80" sortable>
            </el-table-column>
            <el-table-column prop="state" label="状态" min-width="80" sortable>
                <template scope="scope">
                    <span style="color: green" v-if="scope.row.state === 1">启用</span>
                    <span style="color: red" v-else-if="scope.row.state === 0">禁用</span>
                    <span style="color: grey" v-else>未知</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" min-width="100" >
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize"
                           :total="total"
                           style="float:right;">
            </el-pagination>
        </el-col>

        <!-- 新增或编辑界面
            :visible.sync 控制当前窗体是否显示
              :rules  表单校验引用规则
              ref=  给当前表单取一个引用别名
        -->
        <!-- :title，前面加一个":"，绑定title的值 -->
        <el-dialog :title="title" :visible.sync="empFormVisible" :close-on-click-modal="false">
            <el-form :model="empForm" label-width="100px" :rules="empFormRules" ref="empForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="empForm.username" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="empForm.email" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="empForm.phone" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="empForm.age" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="empForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="empFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="empSubmit" :loading="listLoading">提交</el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>

    export default {
        data() {
            return {
                title:"",
                filters: {
                    keyword: ''		/* 用于高级查询的关键字 */
                },
                employees: [],
                total: 0,
                currentPage: 1,
                pageSize: 5,
                page: 1,
                listLoading: false,
                sels: [],//列表选中行

                empFormVisible: false,//编辑界面是否显示
                petTypeLoading: false,
                empFormRules: {// 部门表单属性的校验规则
                    username: [
                        { required: true, message: '请输入员工姓名', trigger: 'blur' }	/* blur模糊 */
                    ],
                    email: [
                        { required: true, message: '请输入员工邮箱', trigger: 'blur' }
                    ],
                    phone: [
                        { required: true, message: '请输入员工电话', trigger: 'blur' }
                    ],
                    age: [
                        { required: true, message: '请输入员工年龄', trigger: 'blur' }
                    ],
                    state: [
                        { required: true, message: '请输入员工状态', trigger: 'blur' }
                    ],
                },
                //编辑界面数据
                empForm: {
                    id: null,
                    username: "",
                    email: "",
                    phone: "",
                    age: null,
                    state: 1
                },
            }
        },
        methods: {
            handleCurrentChange(val) {	/* 处理当前页 */
                this.currentPage = val;
                this.getEmployee();	/* 重新获取 */
            },
            //获取部门列表
            getEmployee() {
                let para = {
                    currentPage: this.currentPage,
                    keyword: this.filters.keyword,
                    pageSize:this.pageSize
                };
                this.listLoading = true;//开启忙等框
                this.$http.post("/emp",para)
                    .then(result=>{
                        result = result.data;//PageList{total rows}
                        this.employees = result.rows;
                        this.listLoading = false;//关闭忙等框
                        this.total = result.total;

                    })
                    .catch(result=>{
                        console.log(result);
                        alert("系统错误！");
                    })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '温馨提示', {
                    type: 'warning'
                }).then(() => {//点击确定
                    this.listLoading = true;//开启忙等框
                    //NProgress.start();
                    this.$http.delete("/emp/"+row.id)	/* 拼接，根据id删除 */
                        .then(result=>{
                            // 不管是否成功都要先关闭忙等框
                            this.listLoading = false;
                            //判断业务操作是否正常
                            result  = result.data;//AjaxResult  {success:false,message:操作失败}
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,//提示我们自己的业务错，有可能已经删除了
                                    type: 'error'
                                });
                            }
                            //2.刷新页面
                            //是否需要重新刷新页面   要  定位到第一页
                            // this.currentPage = 1;
                            this.getEmployee();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            //失败了是否需要重新刷新页面   要  定位到第一页
                            this.page = 1;
                            this.getEmployee();
                        })
                }).catch(() => {//点击取消

                });
            },
            // 显示编辑界面
            handleEdit: function (index, row) {
                this.title = "编辑";
                this.empFormVisible = true;
                //Object.assign 克隆
                this.empForm = Object.assign({}, row);//回显
            },
            // 显示新增界面
            handleAdd: function () {
                this.title = "新增";// 给新增界面添加标题区分编辑页面
                this.empFormVisible = true;// 显示新增框，true
                this.empForm = {//清空历史遗留数据
                    id: null,
                    username: "",
                    email: "",
                    phone: "",
                    age: null,
                    state: 1
                };
            },
            // 提交保存表单的方法
            empSubmit: function () {
                // 表单校验是不是符合要求
                this.$refs.empForm.validate((valid) => {
                    if (valid) {// 符合
                        this.$confirm('确认保存吗？', '提示', {}).then(() => {
                            this.petTypeLoading = true;// 打开按钮的忙等框
                            //NProgress.start();
                            let para = Object.assign({}, this.empForm);

                            this.$http.put("/emp",para).then((result) => {
                                result = result.data;
                                this.listLoading = false;// 无论如何先关闭忙等框
                                this.empFormVisible = false;// 关闭弹出框
                                //NProgress.done();
                                if(result.success){// 如果成功
                                    this.$message({
                                        message: '操作成功',
                                        type: 'success'
                                    });
                                } else {
                                    this.$message({
                                        message: result.message,// 可能是成功了，但是其他代码哪里写错了
                                        type: 'error'
                                    });
                                }
                                // 刷新并且重新定位到第一页
                                // this.Page = 1;
                                this.getEmployee();

                            })
                                .catch(result=>{
                                    this.petTypeLoading = false;// 先关闭忙等框
                                    this.$message({
                                        message: '操作失败',
                                        type: 'error'
                                    });
                                })

                        });
                    }
                });
            },
            selsChange: function (sels) {//当前已经选中的所有行的数据
                this.sels = sels;
            },
            // 批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning',
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    this.$http.patch("/petType",ids)
                        .then(result=>{
                            this.listLoading = false;	// 关闭忙等矿
                            result  = result.data;
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else{
                                this.$message({
                                    message: result.message,// 可能是成功了，但是其他业务代码哪里写错了
                                    type: 'error'
                                });
                            }
                            // 刷新并重新定位到第一页
                            this.getEmployee();
                        })
                        .catch(result=>{
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            // 刷新并重新定位到第一页
                            // this.page = 1;
                            this.getEmployee();
                        })

                }).catch(() => {

                });
            },
        },
        mounted() {	// 钩子函数，页面加载完毕除触发
            this.getEmployee();
        }
    }

</script>

<style scoped>


</style>